package sg.edu.nus.iss.jass.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DebugGraphics;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import sg.edu.nus.iss.jass.constant.ActionBtnContants;
import sg.edu.nus.iss.jass.constant.CommonConstants;
import sg.edu.nus.iss.jass.constant.ErrorConstants;
import sg.edu.nus.iss.jass.controller.JobAdvertisementController;
import sg.edu.nus.iss.jass.controller.MainController;
import sg.edu.nus.iss.jass.dto.Company;
import sg.edu.nus.iss.jass.dto.GeographicalLocation;
import sg.edu.nus.iss.jass.dto.JobAdvert;
import sg.edu.nus.iss.jass.dto.Skill;
import sg.edu.nus.iss.jass.dto.SkillReference;
import sg.edu.nus.iss.jass.privilege.JobSeekerGuestRole;
import sg.edu.nus.iss.jass.privilege.JobSeekerRole;
import sg.edu.nus.iss.jass.privilege.Role;
import sg.edu.nus.iss.jass.privilege.SessionContext;
import sg.edu.nus.iss.jass.util.Util;

//import com.sun.java.swing.plaf.motif.MotifBorders.BevelBorder;

public class JobAdvertisementDetailFormPanel extends FormPanel {
	/**
	 * author: Tan Pic Cheng
	 */
	private static final long serialVersionUID = 1L;
	private JobAdvertisementController jobAdvertisementController;
	private MainFrame mainframe;
	
	private List<Skill> skillList;
	private List<SkillReference> skillsList = null;
	private List<GeographicalLocation> locations = null;
	private List<Company> companies = null;
		
	private JLabel createdDateLbl;
	private JLabel jobTitleLbl; 
	private JLabel jobPositionLbl;
	private JLabel jobDescriptionLbl;
	private JLabel locationCountryLbl;
	private JLabel companyLbl;
	private JLabel startDateLbl;
	private JLabel salaryLbl;
	private JLabel hideSalaryLbl;
	private JLabel displayCreatedDateLbl;
	private JLabel displayHideSalaryLbl;
	private JLabel displaySalaryLbl;
	private JLabel displayCompanyLbl;
	private JLabel displayCountryLbl;
	private	JLabel  skillListLbl[];
	private JLabel  skillLevelLbl[]; 	
			
	private JTextField jobTitleTxtField; 
	private JTextField positionTxtField;
	private JTextField startDateTxtField;
	private JTextField skillExpTxtField[];
	
	private JTextArea descriptionTxtArea;
	private int skillSize = 0;
		
	private JobAdvert jobAdvert = null;
	
	String skillLevels[] = {"Select","Beginner", "Intermediate", "Advanced"};
	private boolean isJobSeeker;
	private boolean isAdvertiser;
	
	public JobAdvertisementDetailFormPanel(MainFrame mainframe, ActionPanel actionPanel,
			HeaderPanel headerPanel, JobAdvert jobAdvert) {
		super(mainframe, actionPanel, headerPanel);
		this.mainframe = mainframe;
		this.jobAdvert = jobAdvert;
		if (SessionContext.getUser().getRole() instanceof JobSeekerGuestRole ||
				SessionContext.getUser().getRole() instanceof JobSeekerRole) {
			this.isJobSeeker = true; 
		}else{
			this.isAdvertiser = true; 
		}
		if(jobAdvert != null){
			initGUI();
			setInitData();
   		}
        else{
        	clearMsg();
        	showMsg(ErrorConstants.ERROR_NO_DATA, false);        	
        }
	}
	private void initGUI() {
        try {
        	setPreferredSize(new Dimension(800, 1550));
        	this.setLayout(null);
        	this.setDebugGraphicsOptions(DebugGraphics.BUFFERED_OPTION);
        	this.setFont(new java.awt.Font("Arial",0,12));
            {
            	createdDateLbl = new JLabel("Created Date:");
            	{
                	createdDateLbl = new JLabel("Created Date:");
                	createdDateLbl.setBounds(22, 29, 91, 25);
                	this.add(createdDateLbl);
                }
                {	
                	displayCreatedDateLbl = new JLabel();                
                	displayCreatedDateLbl.setBounds(159, 29, 150, 25);
                	this.add(displayCreatedDateLbl);
                }
                {
                	jobTitleLbl = new JLabel("Job Title:");
                	jobTitleLbl.setBounds(22, 75, 150, 25);
                	this.add(jobTitleLbl);
                }
                {
                	jobTitleTxtField = new JTextField();
                	jobTitleTxtField.setBounds(159, 73, 150, 25);
                	jobTitleTxtField.setColumns(10);
                	jobTitleTxtField.setEditable(false);
                	this.add(jobTitleTxtField);
                }
                {			
                	jobPositionLbl = new JLabel("Job Position:");
                	jobPositionLbl.setBounds(20, 123, 148, 25);
                	this.add(jobPositionLbl);
                }
                {			
                	positionTxtField = new JTextField();
                	positionTxtField.setBounds(159, 121, 150, 25);
                	positionTxtField.setColumns(10);
                	positionTxtField.setEditable(false);
                	this.add(positionTxtField);
                }
                {			
                	jobDescriptionLbl = new JLabel("Job Description:");
                	this.add(jobDescriptionLbl);
                	jobDescriptionLbl.setBounds(19, 173, 150, 25);
                }
                {			
                	descriptionTxtArea = new JTextArea();
                	descriptionTxtArea.setBounds(159, 173, 150, 139);
                	descriptionTxtArea.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
                	//descriptionTxtArea.setWrapStyleWord(true);
                	descriptionTxtArea.setEditable(false);
                	this.add(descriptionTxtArea);
                }     	
                {	JPanel skillsPanel = createSkillsPanel();
                	setSkillsData();
	             	skillsPanel.setBounds(22, 330, 650, 300);
	        		this.add(skillsPanel);	        		
                }        		
	            {			
	            	locationCountryLbl = new JLabel("Location Country:");
	            	locationCountryLbl.setBounds(361, 173, 150, 25);
	            	this.add(locationCountryLbl);
	            }
	            {	displayCountryLbl = new JLabel();
	            	displayCountryLbl.setBounds(557, 171, 150, 25);
	            	this.add(displayCountryLbl);
	            }
	            {			
	            	companyLbl = new JLabel("Company Name:");
	            	companyLbl.setBounds(361, 220, 150, 25);
	            	this.add(companyLbl);
	            }
	            {	displayCompanyLbl = new JLabel();
	    			displayCompanyLbl.setBounds(557, 218, 150, 25);
	    			this.add(displayCompanyLbl);
	            }               
	            {			
	            	startDateLbl = new JLabel("Start Date:");
	            	startDateLbl.setBounds(361, 32, 150, 25);
	            	this.add(startDateLbl);
	            }
	            {			
	            	startDateTxtField = new JTextField();
	            	startDateTxtField.setBounds(557, 29, 150, 25);
	            	startDateTxtField.setColumns(10);
	            	startDateTxtField.setEditable(false);
	            	this.add(startDateTxtField);
	            }
	            if ((isJobSeeker && !jobAdvert.getIsHideSalary()) || isAdvertiser)
	            {
	            	salaryLbl = new JLabel("Salary:");
	            	salaryLbl.setBounds(361, 77, 150, 25);
	            	this.add(salaryLbl);
	            }
	            if ((isJobSeeker && !jobAdvert.getIsHideSalary()) || isAdvertiser)
	            {			
	            	displaySalaryLbl = new JLabel();
	            	displaySalaryLbl.setBounds(557, 73, 150, 25);
	            	this.add(displaySalaryLbl);
	            }   
	            if (isAdvertiser)
	            {			
					hideSalaryLbl = new JLabel("Hide Salary:");
					hideSalaryLbl.setBounds(361, 123, 150, 25);
					this.add(hideSalaryLbl);
				}
	            if (isAdvertiser)
				{
					displayHideSalaryLbl = new JLabel();
					displayHideSalaryLbl.setBounds(557, 121, 150, 25);
					this.add(displayHideSalaryLbl);
				}
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}
private void setInitData(){
	
	displayCreatedDateLbl.setText(Util.getStringFromdate(jobAdvert.getCreation()));
	jobTitleTxtField.setText(jobAdvert.getTitle());
	positionTxtField.setText(jobAdvert.getPosition());
	startDateTxtField.setText(Util.getStringFromdate(jobAdvert.getStartDate()));
	descriptionTxtArea.setText(jobAdvert.getDescription());
	     	
    displayCompanyLbl.setText(jobAdvert.getCompany().getName());       			
	displayCountryLbl.setText(jobAdvert.getGeographicalLocation().getCountry());
	
	if ((isJobSeeker && !jobAdvert.getIsHideSalary()) || isAdvertiser) {
		boolean hideSalaryOption = jobAdvert.getIsHideSalary();
		String hideOption = ((hideSalaryOption) ? "Yes": "No");
		displayHideSalaryLbl.setText(hideOption);
		int minSalary = (int) jobAdvert.getMinSalary();
		int maxSalary = (int) jobAdvert.getMaxSalary();

		StringBuffer sb = new StringBuffer();
		sb.append(minSalary).append("-").append(maxSalary);
		String salaryRange = sb.toString(); 
		displaySalaryLbl.setText(salaryRange); 
	}
	
}
private JPanel createSkillsPanel() {
		
		JPanel pane = new JPanel(new GridLayout ());
		
		pane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1.5;
		c.weighty = 0.5;
		c.insets = new Insets(5, 5, 5, 5);
		c.ipady = 0;		
		
	    c.gridx = 0;
		c.gridy = 0;
		pane.add(new JLabel("Skill"), c);
		
		c.gridx = 1;
		c.gridy = 0;
		pane.add(new JLabel("Skill Level"), c);

		c.gridx = 2;
		c.gridy = 0;
		pane.add(new JLabel("Years Of Experience"), c);
	
		List<Skill> tempSkillList = jobAdvert.getSkills();
		if(!Util.isEmpty(tempSkillList))
			skillSize = tempSkillList.size();
		
		int size = 5;
		
		//System.out.println(skillSize);

		skillListLbl = new JLabel[size];
		skillLevelLbl = new JLabel[size];
		skillExpTxtField = new JTextField[size];

		int y = 1;

		for (int i = 0; i < size; i++) {
			skillListLbl[i] = new JLabel();
			c.gridx = 0;
			c.gridy = y;
			pane.add(skillListLbl[i], c);

			skillLevelLbl[i] = new JLabel();
			c.gridx = 1;
			c.gridy = y;
			pane.add(skillLevelLbl[i], c);

			skillExpTxtField[i] = new JTextField ();
			if(i < skillSize)
			skillExpTxtField[i].setEditable(false);
			else
				skillExpTxtField[i].setVisible(false);			
			c.ipady = 10;
			c.gridx = 2;
			c.gridy = y;
			pane.add(skillExpTxtField[i], c);
			y++;		
		}
		return pane;
}
private void setSkillsData() {
	
		skillList = jobAdvert.getSkills();
		
		String skillName = null;
		int skillLvl = 0;
		String skillYrs = null;	
		
		int i = 0;
		
		if(skillList != null && skillList.size() > 0){		

			for(Skill s : skillList){		
				skillName = s.getSkillRef().getName().toString();
				skillLvl = s.getLvl();
				skillYrs = Integer.toString(s.getYearOfExp());

				skillListLbl[i].setText(skillName);
				skillLevelLbl[i].setText(skillLevels[skillLvl]);
				skillExpTxtField[i].setText(skillYrs);					
				i++;
			}

		}		
		return;
	}
	public void setController(JobAdvertisementController jobAdvertisementController){
		this.jobAdvertisementController = jobAdvertisementController;
		//this.addJobPanel.setJobAdvertisementController(jobAdvertisementController);
	}	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals(ActionBtnContants.APPLY_JOB)) {
			applyJob();
		} else if (e.getActionCommand().equals(ActionBtnContants.CANCEL)) {
			MainController.getInstance().getJobSearchController().startSearchJob();
		}
	}
	@Override
	public void show() {
		// TODO Auto-generated method stub
		addFormPanel();
	}
	
	public void applyJob(){
		System.out.println("jobAdvert getDescription:" + jobAdvert.getDescription());
		System.out.println("jobAdvert getId:" + jobAdvert.getId());
		jobAdvertisementController.startApplyJob(jobAdvert.getId());
	}
}
